package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author :蕉绿
 * @Date : 2023/3/16 19:55
 * @Version : V1.0
 * @Description :
 */
@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request) {
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if(userinfo == null) {
            return AjaxResult.fail(-1,"非法请求");
        }

        List<Articleinfo> list = articleService.getMyList(userinfo.getId());
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request) {
        if(id == null || id <= 0) {
            //参数有误
            return AjaxResult.fail(-1,"参数异常");

        }
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if(userinfo==null) {
            return AjaxResult.fail(-2,"用户未登录");
        }
        return AjaxResult.success(articleService.del(id,userinfo.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id) {
        if(id == null || id <= 0) {
            return AjaxResult.fail(-1,"非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/incr-rcount")
    public AjaxResult incrRCount(Integer id) {
        if(id!=null && id>0) {
            return AjaxResult.success(articleService.incrRCount(id));
        }
        return AjaxResult.fail(-1,"非法参数");

    }

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request,Articleinfo articleinfo) {
        //1.非空校验
        if(articleinfo == null || !StringUtils.hasLength(articleinfo.getTitle())
        || !StringUtils.hasLength(articleinfo.getContent())) {
            return AjaxResult.fail(-1,"非法参数");
        }
        //2.数据库添加操作
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if(userinfo==null || userinfo.getId() <= 0) {
            return AjaxResult.fail(-2,"非法参数");
        }
        articleinfo.setUid(userinfo.getId());
        //添加数据库并返回结果
        return AjaxResult.success(articleService.add(articleinfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request,Articleinfo articleinfo){
        //非空校验
        if(articleinfo==null || !StringUtils.hasLength(articleinfo.getTitle()) ||
              !StringUtils.hasLength(articleinfo.getContent()) ||
              articleinfo.getId() == null) {
            return AjaxResult.fail(-1,"非法参数");
        }
        //得到当前登录用户
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if (userinfo == null && userinfo.getId()==null) {
            return AjaxResult.fail(-2,"无效用户");
        }
        //修改文章归属人判定的问题
        articleinfo.setUid(userinfo.getId());
        articleinfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleinfo));
    }
    /**
    *@Description:
    *@Param:
    *return:
    */
    @RequestMapping("/listbypage")
    public AjaxResult getListByPage(Integer pindex,Integer psize) {
        //1.参数校正
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if(psize == null || psize < 1) {
            psize = 2;
        }
        //分页公式 (当前页码-1)*每页显示条数
        int offset = (pindex - 1)*psize;
        List<Articleinfo> list = articleService.getListByPage(psize,offset);
        //当前列表总共有多少页
        //多少条
        int totalCount = articleService.getCount();
        double pcountdb = totalCount / (psize  * 1.0);
        int pcount = (int)Math.ceil(pcountdb);
        HashMap<String,Object> result = new HashMap<>();
        result.put("list",list);
        result.put("pcount",pcount);

        return AjaxResult.success(result);
    }

}
